package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Reference
    UserInfoService userInfoService;


    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session != null) {
            session.removeAttribute("USER");
        }
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo,  HttpServletRequest request){
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();

        //校验参数
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //校验账号
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (userInfo == null) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //校验密码
        if (!MD5.encrypt(password).equals(userInfo.getPassword())) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //校验是否被禁用
        if (userInfo.getStatus() == 0) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }

        request.getSession().setAttribute("USER", userInfo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", userInfo.getPhone());
        map.put("nickName", userInfo.getNickName());
        return Result.ok(map);
    }


    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpServletRequest request) {
        String code = registerVo.getCode();
        String nickName = registerVo.getNickName();
        String password = registerVo.getPassword();
        String phone = registerVo.getPhone();
        //1.数据校验(数据的合法性)
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(nickName) || StringUtils.isEmpty(phone)) {

            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //2.手机验证码是否有效（校验码过期）
        //3.输入的校验码和发送的验证码是否一致
        Object sendCode = request.getSession().getAttribute("CODE");
        if (sendCode == null || !code.equals(sendCode)) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }

        //6.手机号是否被占用
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (userInfo != null) {

            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }

        //5.封装数据，保存数据
        userInfo = new UserInfo();
        BeanUtils.copyProperties(registerVo, userInfo);
        userInfo.setStatus(1);
        userInfo.setPassword(MD5.encrypt(password));
        userInfoService.insert(userInfo);
        return Result.ok();

    }


    @RequestMapping("/getCode/{phone}")
    public Result getCode(@PathVariable("phone") String phone, HttpServletRequest request) {
        //模拟发送手机验证码，后续电商系统会有正式使用

        String code = "1111";
        request.getSession().setAttribute("CODE", code);
        return Result.ok(code);

    }
}
